package com.hzit.manager.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.io.StringReader;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzit.common.utils.PageUtils;
import com.hzit.common.utils.Query;

import com.hzit.manager.dao.ContentDao;
import com.hzit.manager.entity.ContentEntity;
import com.hzit.manager.service.ContentService;


@Service("contentService")
public class ContentServiceImpl extends ServiceImpl<ContentDao, ContentEntity> implements ContentService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ContentEntity> page = this.page(
                new Query<ContentEntity>().getPage(params),
                new QueryWrapper<ContentEntity>()
        );

        return new PageUtils(page);
    }

    // @CacheEvict(value = "contents",key = "'findAll2'")
    @CacheEvict(value = "contents",allEntries = true)     // 清空指定名称空间下的所有的key
    @CachePut(value = "content",key = "#root.methodName") // 每次方法都会调用，发现在redis中如果有指定空间+key的数据，会自动与修改后的数据库数据进行同步
    public ContentEntity add(ContentEntity content) {
        // redisTemplate.delete("contentList");
        System.out.println("正在清理缓存【add】。。。");
        this.save(content);
        return content;
    }

    @Override
    // @CacheEvict(value = "contents",key = "'findAll2'")
    @CacheEvict(value = "contents",allEntries = true)
    @CachePut(value = "content",key = "'add'")
    public ContentEntity update(ContentEntity content) {
        // redisTemplate.delete("contentList");
        System.out.println("正在清理缓存【update】。。。");
        this.updateById(content);
        return content;
    }

    @Override
    // @CacheEvict(value = "contents",key = "'findAll2'")
    @Caching(evict = {                  // 适合删除指定名称空间下指定的key，可以有多个
            @CacheEvict(value = "contents",key = "'findAll2'"),
            // @CacheEvict(value = "contents",key = "'findAll3'"),
    },put = {
            @CachePut(value = "content",key = "'add'")
    })
    public void delete(List<String> ids) {
        // redisTemplate.delete("contentList");
        System.out.println("正在清理缓存【delete】。。。");
        this.removeByIds(ids);
    }

    @Cacheable(value = "content",key = "#root.methodName")
    @Override
    public ContentEntity findById(String id) {
        return this.getById(id);
    }

    //2. 将广告列表放到redis中
	@Override
	public List<ContentEntity> findAll() {
        List<ContentEntity> contentList = null;
        String contents = redisTemplate.opsForValue().get("contents");
        if(StrUtil.isNotBlank(contents)){
            contentList = JSON.parseArray(contents,ContentEntity.class);
        }else{
            System.out.println("正在从数据库中查询数据....");
            contentList = this.list();
            redisTemplate.opsForValue().set("contents",JSON.toJSONString(contentList));
        }
        return contentList;
	}

}